<?php
/**
 * Created by PhpStorm.
 * User: jason
 * Date: 2017/11/1
 * Time: 22:58
 */

namespace Home\Service;


use Think\Model;

class ScoreCommentService extends Model
{
    protected $name = 'score_comment';


    public function comment($scoreId, $page=1, $limit=5)
    {
        $map = array(
            'score_id' => $scoreId,
            'is_delete' => 0
        );
        $count = $this->where($map)->count();
        $list = $this->where($map)->order('create_time')->page($page, $limit)->order('create_time desc')->select();

        $userIds = array_unique(array_column($list, 'user_id'));
        $userModel = new UserService();
        $userMap = $userModel->userinfoByIds($userIds);
        $comment = [];
        foreach ($list as $key => $val) {
            $val['remark'] = htmlspecialchars_decode($val['remark']);
            $comment[$key] = $val;
            $comment[$key]['user_info'] = $userMap[$val['user_id']];
        }
        $result['list'] = $comment;
        $result['count'] = $count;
        return $result;
    }

    public function addComment($param)
    {
        $param['remark'] = addslashes(htmlspecialchars($param['remark']));
        $res = $this->data($param)->add();
        if ($res) {
            $userModel = new UserService();
            $userMap = $userModel->userinfoByIds($param['user_id']);
            $param['user_info'] = $userMap[$param['user_id']];
            $param['create_time'] = date('Y-m-d H:i:s', time());
            return $param;
        } else {
            return false;
        }

    }
}